package com.cyy.demo.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

@Slf4j
@Component
public class KafkaProducer<T> {

    @Autowired
    private KafkaTemplate<T,T> kafkaTemplate;

    @Value("${kafka.topic.name}")
    private String topicName;

    public ListenableFuture<SendResult<T, T>> send(T t){
        return this.send(topicName,t);
    }

    public ListenableFuture<SendResult<T, T>> send(String topicName,T t){
        ListenableFuture<SendResult<T, T>> result = kafkaTemplate.send(topicName, t);
        return result;
    }

}
